package com.binarycube.webserver.datasource;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;

import com.binarycube.webserver.config.DatasourceConfig;

public class FileDataSource extends AbstractDataSource implements IDataSource{
	static Logger log = Logger.getLogger(FileDataSource.class.getName());
	
	public List<Record> doQuery() {
		validateParams();
		BufferedReader abc;
		String line;
		try {
			abc = new BufferedReader(new FileReader(_config.getProperty("file")));
			while((line = abc.readLine()) != null) {
				Record r = Record.IndexedRecord();
				r.add(line);
			    _records.add(r);
			    
			}
			abc.close();
		} catch (IOException e) {
			log.error("Error configuring File Datasource: " + _config.getName(), e);
		}
		
		return _records;
	}
	
	public void configure(DatasourceConfig config) {
		super.configure(config);
		if (!new File(_config.getProperty("file")).exists()) {
			log.error("Error configuring File Datasource - file not found: " + _config.getName());
		}
		
		
		

		
	}

}
